{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "179cc62d",
   "metadata": {},
   "source": [
    "# 百度 Access token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6fa1712c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24.1d3e4f57f1decfabc58fad8d25ed6278.2592000.1681645859.282335-31382282\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "\n",
    "def main():\n",
    "        \n",
    "    url = \"https://aip.baidubce.com/oauth/2.0/token?client_id=wp2YIY8RHk1IVwbvS9NpUt3G&client_secret=YQeBuNcIdGHPu2Fb6DdoVftab5oNiclD&grant_type=client_credentials\"\n",
    "    \n",
    "    payload = \"\"\n",
    "    headers = {\n",
    "        'Content-Type': 'application/json',\n",
    "        'Accept': 'application/json'\n",
    "    }\n",
    "    \n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    \n",
    "    print(response.json()[\"access_token\"])\n",
    "    \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "224f69b5",
   "metadata": {},
   "source": [
    "# 通用物体识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "14d002b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '杯子', 'score': 0.831423, 'root': '商品-容器'}, {'keyword': '茶杯', 'score': 0.592257, 'root': '商品-容器'}, {'keyword': '陶瓷/马克杯', 'score': 0.387113, 'root': '商品-厨具/餐具'}, {'keyword': '陶瓷杯', 'score': 0.201363, 'root': '商品-容器'}, {'keyword': '杯具', 'score': 0.027904, 'root': '商品-日用品'}], 'log_id': 1636717624396886571}\n"
     ]
    }
   ],
   "source": [
    "# 物体识别\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(\"D:\\桌面\\杯子.jpg\", 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.1d3e4f57f1decfabc58fad8d25ed6278.2592000.1681645859.282335-31382282'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "0bb37dc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'score': 0.98271304, 'name': '芫荽'}, {'score': 0.50988233, 'name': '旱芹'}, {'score': 0.11402481, 'name': '香芹'}], 'log_id': 1636720746421220627}\n"
     ]
    }
   ],
   "source": [
    "# 植物识别\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "植物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(\"D:\\桌面\\香菜.webp\", 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.1d3e4f57f1decfabc58fad8d25ed6278.2592000.1681645859.282335-31382282'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "543806dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'name': '番茄炒蛋', 'calorie': '86', 'probability': '0.480425', 'has_calorie': True}, {'name': '炒鸡蛋', 'calorie': '195', 'probability': '0.433514', 'has_calorie': True}, {'name': '盖浇饭', 'probability': '0.0536439', 'has_calorie': False}, {'name': '鸡蛋面', 'calorie': '225', 'probability': '0.00734567', 'has_calorie': True}, {'name': '非菜', 'probability': '0.00416583', 'has_calorie': False}], 'log_id': 1636723169831757026}\n"
     ]
    }
   ],
   "source": [
    "# 菜品识别\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(\"D:\\桌面\\番茄炒蛋.webp\", 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.1d3e4f57f1decfabc58fad8d25ed6278.2592000.1681645859.282335-31382282'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "f43c71eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'score': '0.80445', 'name': '金刚鹦鹉'}, {'score': '0.129503', 'name': '金刚鹦鹉族'}, {'score': '0.0196716', 'name': '五彩金刚鹦鹉'}, {'score': '0.0111042', 'name': '五彩鹦鹉'}, {'score': '0.00333999', 'name': '黑头织雀'}, {'score': '0.00268424', 'name': '红色的鹦鹉'}], 'log_id': 1636724780453033678}\n"
     ]
    }
   ],
   "source": [
    "# 动物识别\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "动物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/animal\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(\"D:\\桌面\\鸟.webp\", 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.1d3e4f57f1decfabc58fad8d25ed6278.2592000.1681645859.282335-31382282'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc6611e2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
